package webs.servlet.ServletDemoOne;
//JDBC工具类

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class jdbcToolClass
{
    public static String url;
    public static String user;
    public static String password;
    public static String driver;
    static
    {
        try
        {
            Properties proper=new Properties();
            ClassLoader classLoader=jdbcToolClass.class.getClassLoader();
            InputStream is=classLoader.getResourceAsStream("JDBC.properties");
            proper.load(is);
            url=proper.getProperty("url");
            user=proper.getProperty("user");
            password=proper.getProperty("password");
            driver=proper.getProperty("driver");
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        try
        {
            Class.forName(driver);
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
    public static Connection Register() throws SQLException
    {
        return DriverManager.getConnection(url,user,password);
    }
    public static void close(Statement sta, Connection con)
    {
        if (sta!=null)
        {
            try
            {
                sta.close();
            } catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
        }
        if (con!=null)
        {
            try
            {
                con.close();
            } catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
        }
    }
    public static void close(Statement sta, Connection con, ResultSet result)
    {
        if (result!=null)
        {
            try
            {
                result.close();
            } catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
        }
        close(sta,con);
    }
}
